package ArrayList;

import org.junit.Test;

/**
 * @Classname JZ42连续子数组的最大和
 * @Description  f(i)=Max（f(i-1)+nums[i],nums[i]）
 * @Date 2022/12/7 14:58
 * @Created by xjl
 */
public class JZ42连续子数组的最大和 {
    /**
     * @description 采用result=Math.max(nums[i],result)
      * @param: nums
     * @date: 2022/12/7 14:58
     * @return: int
     * @author: xjl
    */
    public int maxSubArray(int[] nums) {
        int result = nums[0];
        for (int i = 1; i < nums.length; i++) {
            nums[i] = Math.max(nums[i], nums[i-1] + nums[i]);
            result=Math.max(result,nums[i]);
        }
        return result;
    }

    @Test
    public void test(){
        int i = maxSubArray(new int[]{-2, 1, -3, 4, -1, 2, 1, -5, 4});
        System.out.println(i);
    }
}
